package model;

import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.ImageIcon;

import main.Game;
import view.Ligne_view;
import controler.DiceListener;
import controler.EndTurnListener;

public class Player_bot extends Player{
	Game g;
	Ligne_view view;
	public Player_bot(String name, ImageIcon p) {
		super(name, p);
		setBot();
	}
	public void doTurn(Game game, Ligne_view game_view){
		this.g=game;
		this.view=game_view;


		//bot play his turn
		DiceListener dl =new DiceListener(g, view);

		//if player can replay, he must do it

		while(view.throw_dice.isEnabled()){
			dl.actionPerformed(new ActionEvent(view.throw_dice, 0, "Throw Dices"));
			if(game.actual_player.getPosition().kindOfCell.equals("city")||game.actual_player.getPosition().kindOfCell.equals("station")||game.actual_player.getPosition().kindOfCell.equals("utility")){
				if(game.actual_player.getPosition().get_owner().getName().equals("bank")){
					if(game.actual_player.getAccountBalance()>(game.actual_player.getPosition().getPrice()+100)){
						game.buyPlace();
					}
				}
			}
		}

		
		if(view.buyHouse.isEnabled() && !game.actual_player.getPrisonerStatut()){
			//we check all the card of the actual bot
			City c=new City();
			Cell cell=null;
			//we look inside the full family of the player
			for(int i=0; i<game.actual_player.have_full_family().size();i++){
				//we find the cell of the card
				for(int y=0; y<Board.map.length;y++){
					if(Board.map[y].getName().equals(c.getFamily(game.actual_player.have_full_family().get(i)).get(0).name)){
						//if this is not our only full family
						if(cell!=null){

							//if their is less than an hotel on the cell
							if(Board.map[y].getNbHouses()<5){
								//we take the high price
								if(c.getFamily(game.actual_player.have_full_family().get(i)).get(0).house_cost>c.house_cost){
									cell=Board.map[y];
									c=c.getFamily(game.actual_player.have_full_family().get(i)).get(0);
								}
							}
						}
						else{
							//if their is no hotel on this cell
							if(Board.map[y].getNbHouses()<5){
								cell=Board.map[y];
								c=c.getFamily(game.actual_player.have_full_family().get(i)).get(0);
							}
						}						
					}
				}
			}
			if(c.house_cost!=0){
				int nbHouse = game.actual_player.getAccountBalance()/c.house_cost;
				ArrayList<City>group = c.getFamily(c.family);
				//if the family size is 2
				if(group.size()==2){
					//if we can buy more than 1 house
					if(nbHouse/2>=1){

						//if nbHouse of the cell and the houses that we can buy > or = 5, we buy an hotel 
						if(cell.getNbHouses()<5){
							if((cell.getNbHouses()+nbHouse/2)>=5){
								game.bank.payBank(game.actual_player, ((5-cell.getNbHouses())*c.house_cost)*2);
								game.setHouse(group.get(0).name, 5);
								game.setHouse(group.get(1).name, 5);
							}

							else{
								game.bank.payBank(game.actual_player, (nbHouse/2*2*c.house_cost));
								int nbHouseToBuild = cell.getNbHouses()+(nbHouse/2);
								game.setHouse(group.get(0).name, nbHouseToBuild);
								game.setHouse(group.get(1).name, nbHouseToBuild);
							}
						}
					}
				}
				///if the family size is 3
				else if(group.size()==3){
					//if we can buy more than 3 houses
					if(nbHouse/3>=1){

						//if nbHouse of the cell and the houses that we can buy > or = 5, we buy an hotel 
						if(cell.getNbHouses()<5){
							if((cell.getNbHouses()+(nbHouse/3))>=5){
								game.bank.payBank(game.actual_player, ((5-cell.getNbHouses())*c.house_cost)*3);
								game.setHouse(group.get(0).name, 5);
								game.setHouse(group.get(1).name, 5);
								game.setHouse(group.get(2).name, 5);
							}

							else{
								game.bank.payBank(game.actual_player, nbHouse/3*3*c.house_cost);
								int nbHouseToBuild = cell.getNbHouses()+(nbHouse/3);
								game.setHouse(group.get(0).name, nbHouseToBuild);
								game.setHouse(group.get(1).name, nbHouseToBuild);
								game.setHouse(group.get(2).name, nbHouseToBuild);
							}
						}
					}
				}
			}
		}

		finishTurn();
	}
	//end of turn
	public void finishTurn(){
		EndTurnListener end = new EndTurnListener(g, view);
		end.actionPerformed(new ActionEvent(view.finish_turn, 0, "I am done for now"));
	}
}